<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Admin_Settings extends Controller_Admin_Base
{
    public function action_index()
    {        
        /*$mailbox_hash = $this->request->param('mailbox_hash') ? $this->request->param('mailbox_hash') : null;
        $mailbox      = ORM::factory('mailbox')->get_mailbox_by_hash($mailbox_hash);
        
        $content = new View('admin/mailbox/index_tpl');
             
        $this->print_template($filter['name'].' - '.'Тикеты', $content); */    
    }
    
    public function action_connection()
    { 
        $mailbox_hash = $this->request->param('id') ? $this->request->param('id') : null;
        $mailbox      = ORM::factory('mailbox')->get_mailbox_by_hash($mailbox_hash);
        
        $content = new View('admin/settings/connection_tpl');
        $content->mailbox = $mailbox;       
        $this->print_template('Перенаправление - Настройки', $content, 'settings');  
    }
    
    public function action_patterns()
    { 
        $mailbox_hash = $this->request->param('id') ? $this->request->param('id') : null;
        $mailbox      = ORM::factory('mailbox')->get_mailbox_by_hash($mailbox_hash);
        
        if (HTTP_Request::POST == $this->request->method()) { 
            $pattern_id = $this->request->post('pattern_id', null);
            $name       = trim($this->request->post('name'));
            $reply      = trim($this->request->post('reply'));
            if ($name && $reply) {
                if ($pattern_id) {
                    $pattern = ORM::factory('pattern', $pattern_id);
                    $pattern->edit_pattern($name, $reply);   
                } else {
                    $pattern = ORM::factory('pattern')->add_pattern($mailbox, $name, $reply);
                }
            }
        }
        
        $content = new View('admin/settings/patterns_tpl');
        $content->mailbox  = $mailbox;
        $content->patterns = ORM::factory('pattern')->get_patterns($mailbox);        
        $this->print_template('Шаблоны - Настройки', $content, 'settings');  
    } 
    
    public function action_mailbox()
    { 
        $mailbox_hash = $this->request->param('id') ? $this->request->param('id') : null;
        $mailbox      = ORM::factory('mailbox')->get_mailbox_by_hash($mailbox_hash);
        
        if (HTTP_Request::POST == $this->request->method()) {
            $email = $this->request->post('mailbox_email');
            $name  = $this->request->post('name');
                  
            try {        
                $mailbox->edit_mailbox($email, $name);

                //Request::current()->redirect(Helper_Admin::forward_mailbox_url($mailbox)); 
            } catch (Model_Exception $e) {
                
            }
        }
        
        $content = new View('admin/settings/mailbox_tpl');
        $content->mailbox  = $mailbox;        
        $this->print_template('Редактирование ящика - Настройки', $content, 'settings');  
    } 
    
    public function action_autoreply()
    { 
        $mailbox_hash = $this->request->param('id') ? $this->request->param('id') : null;
        $mailbox      = ORM::factory('mailbox')->get_mailbox_by_hash($mailbox_hash);
        
        $autoreply = null;
        if (HTTP_Request::POST == $this->request->method()) { 
            $active  = $this->request->post('active');
            $subject = trim($this->request->post('subject'));
            $reply   = trim($this->request->post('reply'));
            if ($subject && $reply) {
                $mailbox->is_autoreply_enabled = $active;
                $mailbox->save();
                $autoreply = ORM::factory('autoreply')->save_autoreply($mailbox, $subject, $reply);
            }
        }
        
        if (!$autoreply) {
            $autoreply = ORM::factory('autoreply', $mailbox->id);   
        }
        
        $content = new View('admin/settings/autoreply_tpl');
        $content->mailbox   = $mailbox; 
        $content->autoreply = $autoreply;      
        $this->print_template('Автоответ - Настройки', $content, 'settings');  
    }
} 
